/**
 * 地图定位
 */
var map, markersLayer;
// 添加地物标注
function addMarker(lon, lat, str) {

	if (typeof (map) == "undefined") {
		var vecLayer = new Geo.View2D.Layer.XYZ(
				"高德矢量",
				[
				// "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}",
				// "http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}",
				// "http://webrd03.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}",
				"http://webrd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}"
				// "http://20.0.51.77/mapabc/roadmap/${z}/${x}/${y}.png"
				], {
					isBaseLayer : true,
					visibility : true,
					displayInLayerSwitcher : true
				});
		map = new OpenLayers.Map("map", {
			projection : "EPSG:900913",
			displayProjection : "EPSG:4326",
			units : 'm',
			numZoomLevels : 19
		});
		map.addLayer(vecLayer);
		map.addControl(new OpenLayers.Control.MousePosition());
		markersLayer = new Geo.View2D.Layer.GeoMarkers("GeoMarkers");
		map.addLayer(markersLayer);
	}

	if (typeof (markersLayer) != "undefined") {
		deleteMarker(markersLayer);
	}

	// google地图需要转换坐标
	var lonLat = new OpenLayers.LonLat(lon, lat);
	lonLat.transform(map.displayProjection, map.getProjectionObject());
	map.setCenter(lonLat, 14);
	// 自定义标签
	var html = "<div style='background-color: #aac5ee;padding:2px;border:1px solid #adb09b;word-break:keep-all;white-space:nowrap;'>"
			+ str + "</div>";
	var tag = new Geo.View2D.MarkerTag(html);
	tag.offset = new Geo.Pixel(-(tag.size.w / 2), -tag.size.h - 25);
	// 图标icon
	var url = "../statics/map/img/marker_red.png";
	var size = new Geo.Size(19, 25);
	var offset = new Geo.Pixel(-(size.w / 2), -size.h);
	var icon = new Geo.View2D.Icon(url, size, offset);
	// 标注
	var geoMarker = new Geo.GeoMarker(lonLat, icon, tag)
	markersLayer.addMarker(geoMarker);
	// 设置地图最小缩放级别为17级
	map.events.register("zoomend", this, function(e) {
		if (map.getZoom() < 8) {
			// map.zoomTo(17);
			map.setCenter(lonLat, 8);
		}
	});
}

// 删除地物标注
function deleteMarker(markersLayer) {
	var markers = markersLayer.markers;
	if (markers.length != 0) {
		for (var l = 0; l < markers.length; l++) {
			markersLayer.removeMarker(markers[l]);
		}
		deleteMarker(markersLayer);
	}
}